package com.qianfeng.openplatform.controller;

import com.qianfeng.openplatform.sercice.CacheService;
import com.qianfeng.openplatform.utils.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author 江江
 * @Date 2020/7/22 12:36
 */
@RestController
@RequestMapping("/cache")
public class CacheController {
    @Autowired
    private CacheService cacheService;

    @RequestMapping("/save2Redis")
    public boolean save2Redis(@RequestParam String key, @RequestParam String value,@RequestParam Long expireTime) throws Exception {
        System.out.println(key);
        CheckUtil.check(key);
        CheckUtil.check(value);
        return cacheService.save2Redis(key,value,expireTime);
    }

    @RequestMapping("/getFromRedis/{key}")
    public String getFromRedis(@PathVariable String key) throws Exception {
        CheckUtil.check(key);
        return cacheService.getFromRedis(key);
    }

    @RequestMapping("/deleteKey/{key}")
    public boolean deleteKey(@PathVariable String key) throws Exception {
        CheckUtil.check(key);
        return cacheService.deleteKey(key);
    }

    @RequestMapping("/expire/{key}/{expireTime}")
    public boolean expire(@PathVariable String key, @PathVariable long expireTime) throws Exception {
        CheckUtil.check(key);
        return cacheService.expire(key,expireTime);
    }




    @RequestMapping("/smembers/{key}")
    public Set<Object> sMembers(@PathVariable String key) throws Exception {
        CheckUtil.check(key);
        return cacheService.sMembers(key);
    }

    @RequestMapping("/sadd")
    public Long sAdd(@RequestParam String key,@RequestParam  String value,@RequestParam  Long expireTime) throws Exception {
        CheckUtil.check(key);
        CheckUtil.check(value);
        return cacheService.sAdd(key,value,expireTime);
    }

    @RequestMapping("/saddAll")
    public Long sAdd(@RequestParam String key, @RequestBody String[] value, @RequestParam  Long expireTime) throws Exception {
        CheckUtil.check(key);
        return cacheService.sAdd(key,value,expireTime);
    }

    @RequestMapping("sremove/{key}/{value}")
    public Long sRemove(@PathVariable String key,@PathVariable String value) throws Exception {
        CheckUtil.check(key);
        CheckUtil.check(value);
        return cacheService.sRemove(key,value);
    }

    @RequestMapping("/findKeyByPartten/{partten}")
    public Set<String> findKeyByPartten(@PathVariable String partten) throws Exception {
        CheckUtil.check(partten);
        return cacheService.findKeyByPartten(partten);
    }


    public Long getAutoIncrementId(String key) throws Exception {
        return cacheService.getAutoIncrementId(key);
    }

    public void hSet(String key, String field, String value) throws Exception {
        cacheService.hSet(key,field,value);
    }

    @RequestMapping("hget/{key}/{field}")
    public String hGet(@PathVariable String key, @PathVariable String field) throws Exception {
        CheckUtil.check(key);
        CheckUtil.check(field);
        return cacheService.hGet(key,field);
    }

    @RequestMapping(value = "/hgetall",method = RequestMethod.GET)
    public Map<Object, Object> hGetAll(@RequestParam String key) throws Exception {
        return cacheService.hGetAll(key);
    }

    @RequestMapping(value = "/hmset",method = RequestMethod.POST)
    public boolean hMset(@RequestParam String key,@RequestBody Map<Object,Object> map) throws Exception {

        return cacheService.hMset(key,map);
    }


    public boolean setNX(String key, String value, long expireSecond) throws Exception {
        return    cacheService.setNX(key,value,expireSecond);
    }






    @RequestMapping(value = "/hIncrement/{key}/{field}/{delta}")
    public Long hIncrement(@PathVariable String key,@PathVariable String field,@PathVariable long delta) throws Exception {
        return cacheService.hIncrement(key,field,delta);
    }
}
